package com.sz.biz.logistics.core.service.impl;

import com.google.common.collect.Lists;
import com.sz.biz.logistics.core.entity.ProductCustomerRel;
import com.sz.biz.logistics.core.service.ProductCustomerRelService;
import com.sz.biz.logistics.core.dto.ProductCustomerRelDto;
import com.sz.common.base.dao.ParamData;
import com.sz.common.base.exception.Exceptions;
import com.sz.common.base.service.AbstractService;
import com.sz.common.base.utils.CollectionUtils;
import com.sz.biz.logistics.constants.ErrorCodes;
import com.sz.common.core.service.PrincipalUtils;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Function:  客户可见/不可见产品服务 <br>
 * Author: jifeixing <br>
 * Date: 2017-08-09 14:58:00
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class ProductCustomerRelServiceImpl extends AbstractService implements ProductCustomerRelService {


    @Override
    protected String getMapperNamespace() {
        return "ProductCustomerRelMapper";
    }

    /**
     * 新增
     * @param productCustomerRelDto
     * @return
     */
    @Override
    public void save(ProductCustomerRelDto productCustomerRelDto) {
        if(null == productCustomerRelDto){
            throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_ILLEGAL);
        }
        //保存客户产品组可见性
        List<Integer> productGroupIds = productCustomerRelDto.getProductGroupIds();
        if(!CollectionUtils.isEmpty(productGroupIds)){
            for (Integer groupId : productGroupIds) {
                saveProductCustomerRel(productCustomerRelDto.getCustomerId(), groupId, null, 0, productCustomerRelDto.getWbFlag());
            }
        }
        //保存客户产品可见性
        List<String> productUids = productCustomerRelDto.getProductUids();
        if(!CollectionUtils.isEmpty(productUids)){
            for (String uid : productUids) {
                saveProductCustomerRel(productCustomerRelDto.getCustomerId(), null, uid, 1, productCustomerRelDto.getWbFlag());
            }
        }
        //保存白黑名单信息
        if(CollectionUtils.isEmpty(productGroupIds) && CollectionUtils.isEmpty(productUids)){
            saveProductCustomerRel(productCustomerRelDto.getCustomerId(), null, null, 1, productCustomerRelDto.getWbFlag());
        }
    }

    /**
     * 保存客户产品可见性信息
     * @param customerId
     * @param productGroupId
     * @param productUid
     * @param type
     * @param wbFlag
     */
    private void saveProductCustomerRel(Integer customerId, Integer productGroupId, String productUid, Integer type, Integer wbFlag){
        ProductCustomerRel productCustomerRel = new ProductCustomerRel();
        productCustomerRel.setCustomerId(customerId);
        productCustomerRel.setProductGroupId(productGroupId);
        productCustomerRel.setProductUid(productUid);
        productCustomerRel.setType(type);
        productCustomerRel.setWbFlag(wbFlag);
        productCustomerRel.setCreateUserId(PrincipalUtils.getAccountId());
        productCustomerRel.setLastUpdateUserId(PrincipalUtils.getAccountId());
        dao.save(getSqlName("insertSelective"), productCustomerRel);
    }

    /**
     * 修改
     * @param productCustomerRelDto
     */
    @Override
    public void update(ProductCustomerRelDto productCustomerRelDto) {
        if(null == productCustomerRelDto){
            throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_ILLEGAL);
        }
        //删除
        deleteByCustomerId(productCustomerRelDto.getCustomerId());
        //添加
        save(productCustomerRelDto);
    }

    /**
     * 根据客户删除
     * @param customerId
     */
    private void deleteByCustomerId(Integer customerId){
        ParamData paramData = new ParamData();
        paramData.put("customerId", customerId);
        dao.delete(getSqlName("deleteByCustomerId"), paramData);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void batchDelete(List<Integer> ids) {
        ParamData paramData = new ParamData();
        paramData.put("ids", ids);
        dao.delete(getSqlName("batchDelete"), paramData);
    }

    /**
     * 获取客户可见产品信息
     * @param customerId
     * @param isBlack
     * @return
     */
    @Override
    public List<ProductCustomerRel> findByCustomerId(Integer customerId, Boolean isBlack){
        ParamData paramData = new ParamData();
        paramData.put("customerId", customerId);
        paramData.put("isBlack", isBlack);
        return dao.findForList(getSqlName("findByCustomerId"), paramData, ProductCustomerRel.class);
    }
    /**
     * 根据客户id获取产品信息
     * @param customerId
     * @return
     */
    @Override
    public List findProductsByCustomerId(Integer customerId){
        return dao.findForList(getSqlName("findProductsByCustomerId"), customerId, Map.class);
    }

    /**
     * 根据客户id获取产品组信息
     * @param customerId
     * @return
     */
    @Override
    public List findProductGroupsByCustomerId(Integer customerId){
        return dao.findForList(getSqlName("findProductGroupsByCustomerId"), customerId, Map.class);
    }

    /**
     * 获取可见性标志
     * @param customerId
     * @return
     */
    @Override
    public Integer findWbFlag(Integer customerId){
        return (Integer) dao.findForObject(getSqlName("findWbFlag"), customerId);
    }

    /**
     * 获取客户可见性
     * @param customerId
     * @return
     */
    @Override
    public Map<String, Object> findProductAndGroupsByCustomerId(Integer customerId){
        List<Object> productList = Lists.newArrayList();
        Integer wbFlag =findWbFlag(customerId);
        List products = findProductsByCustomerId(customerId);
        List productGroups = findProductGroupsByCustomerId(customerId);
        if(!CollectionUtils.isEmpty(products)){
            productList.addAll(products);
        }
        if(!CollectionUtils.isEmpty(productGroups)){
            productList.addAll(productGroups);
        }
        Map<String, Object> result = new HashedMap();
        result.put("wbFlag", wbFlag);
        result.put("products", productList);
        return result;
    }
}
